package com.design01_builder.pattern04_builder.example02;

/**
 * @ClassName RabbitMQClient3
 * @Description 建造者模式
 * 建造者使用步骤如下:
 * 1. 目标类的构造方法要传入Builder对象
 * 2. Builder建造者类位于目标类内部,并且使用static修饰
 * 3. Builder建造者对象提供内置的各种set方法,注意set方法返回的是builder对
 * 象本身
 * 4. Builder建造者类提供build()方法实现目标对象的创建
 * @Author zhangjunhao
 * @DATE 2023/9/3 2:10
 */
public class RabbitMQClient3 {


    //私有构造,目标类的构造方法要放入一个bulider对象

    public RabbitMQClient3(Builder builder) {
    }


    //bulider类位于目标类的内部,并且将static修饰,保证不可变通
    public static class Builder {
        //返回构建好的复杂对象
        public  RabbitMQClient3 build() {
            //首先进行校验
            if (mode == 1) { //工作队列模式不需要设计交换机,但是队列名称一定要有
                if (exchange != null) {
                    throw new RuntimeException("工作队列模式无需 设计交换机");
                }
                if (queue == null || queue.trim().equals("")) {
                    throw new RuntimeException("工作队列模式名称 不能为空");
                }
                if (isDurable == false) {
                    throw new RuntimeException("工作队列模式必须 开启持久化");
                }
            } else if (mode == 2) { //路由模式必须设计交换机,但是能设计队列
                if (exchange == null) {
                    throw new RuntimeException("路由模式下必置交换机");
                }
                if (queue != null) {
                    throw new RuntimeException("路由模式无须设计 队列名称");
                }
            }
            return new RabbitMQClient3(this);
        }


        //属性密闭性,保证对象不可变
        private String host = "127.0.0.1";
        private int port = 5672;
        private int mode;
        private String exchange;
        private String queue;
        private boolean isDurable = true;
        int connectionTimeout = 1000;

        public Builder setHost(String host) {
            this.host = host;
            return this;
        }

        public Builder setPort(int port) {
            this.port = port;
            return this;
        }

        public Builder setMode(int mode) {
            this.mode = mode;
            return this;
        }

        public Builder setExchange(String exchange) {
            this.exchange = exchange;
            return this;
        }

        public Builder setQueue(String queue) {
            this.queue = queue;
            return this;
        }

        public Builder setDurable(boolean durable) {
            isDurable = durable;
            return this;
        }

        public Builder setConnectionTimeout(int connectionTimeout) {
            this.connectionTimeout = connectionTimeout;
            return this;
        }


        public void sendMessage(String msg) {
            System.out.println("发送消息......");
        }

    }


    public static void main(String[] args) {

//        RabbitMQClient2 client2 = new RabbitMQClient2();
//        client2.setHost("192.168.52.123");
//        client2.setQueue("queue");
//        client2.setMode(1);
//        client2.setDurable(true);
//        client2.sendMessage("Test-MSG2");

        /**
        * 使用链式编程设置参数
        * */
        RabbitMQClient3 client = new RabbitMQClient3.Builder()
                .setHost("192.168.52.123")
                .setMode(2)
                .setExchange("text-exchange")
                .setPort(5672)
                .setDurable(true)
                //校验
                .build();
        //client.sendMessage("Test");


    }
}








